import Vue from 'vue'
import Router from 'vue-router'
import roleConstant from '@/constant/role'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: '示例菜单',
    meta: { title: '示例菜单', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: '表格',
        component: () => import('@/views/table/index'),
        meta: { title: '表格', icon: 'table' }
      },
      {
        path: 'tree',
        name: '菜单树',
        component: () => import('@/views/tree/index'),
        meta: { title: '菜单树', icon: 'tree' }
      },
      {
        path: 'form',
        name: '表单',
        component: () => import('@/views/form/index'),
        meta: { title: '表单', icon: 'form' }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  {
    path: '外链',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: '外链', icon: 'link' }
      }
    ]
  },
  // 人员管理
  {
    path: '/member',
    component: Layout,
    alwaysShow: true,
    meta: { title: '人员管理', icon: 'user', roles: [roleConstant.eduManager] },
    children: [
      {
        path: 'student',
        component: () => import('@/views/member/student'),
        meta: { title: '学生管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'teacher',
        component: () => import('@/views/member/teacher'),
        meta: { title: '教师管理', roles: [roleConstant.eduManager] }
      }
    ]
  },
  // 组织管理(隶属行政管理)
  {
    path: '/organization',
    component: Layout,
    alwaysShow: true,
    meta: { title: '组织管理', icon: 'tree', roles: [roleConstant.eduManager] },
    children: [
      {
        path: 'college',
        component: () => import('@/views/organization/org/college'),
        meta: { title: '学院管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'major',
        component: () => import('@/views/organization/org/major'),
        meta: { title: '专业管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'edu-class',
        component: () => import('@/views/organization/org/eduClass'),
        meta: { title: '行政班级管理', roles: [roleConstant.eduManager] }
      }
    ]
  },
  // 资源管理(隶属行政管理)
  {
    path: '/resourse',
    component: Layout,
    alwaysShow: true,
    meta: { title: '资源管理', icon: 'resourse', roles: [roleConstant.eduManager] },
    children: [
      {
        path: 'campus',
        component: () => import('@/views/organization/resourse/campus'),
        meta: { title: '校区管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'teach-building',
        component: () => import('@/views/organization/resourse/teachBuilding'),
        meta: { title: '教学楼管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'classroom-type',
        component: () => import('@/views/organization/resourse/classroomType'),
        meta: { title: '教室类型管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'classroom',
        component: () => import('@/views/organization/resourse/classroom'),
        meta: { title: '教室管理', roles: [roleConstant.eduManager] }
      }
    ]
  },
  // 课程管理
  {
    path: '/course',
    component: Layout,
    alwaysShow: true,
    meta: { title: '课程管理', icon: 'course', roles: [roleConstant.eduManager, roleConstant.teachingSecretary] },
    children: [
      {
        path: 'basic',
        component: () => import('@/views/course/course/courseBasic'),
        meta: { title: '课程基本信息管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'course-schedule-query',
        component: () => import('@/views/course/course/courseSchedule/courseScheduleQuery'),
        meta: { title: '开课信息查询', roles: [roleConstant.eduManager] }
      },
      {
        path: 'course-schedule',
        component: () => import('@/views/course/course/courseSchedule'),
        meta: { title: '开课管理', roles: [roleConstant.teachingSecretary] }
      },
      {
        path: 'class-course',
        component: () => import('@/views/course/course/classCourse'),
        meta: { title: '班级必修课管理', roles: [roleConstant.teachingSecretary] }
      }
    ]
  },
  // 选课管理
  {
    path: '/course-selection',
    component: Layout,
    alwaysShow: true,
    meta: { title: '选课管理', icon: 'course-choose', roles: [roleConstant.eduManager] },
    children: [
      {
        path: 'switch',
        component: () => import('@/views/course/courseSelection/chooseSwtich'),
        meta: { title: '选课配置管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'choose-course',
        component: () => import('@/views/course/courseSelection/chooseApprove'),
        meta: { title: '选课确认管理', roles: [roleConstant.eduManager] }
      }
    ]
  },
  // 排课管理
  {
    path: '/course-schedule',
    component: Layout,
    alwaysShow: true,
    meta: { title: '排课管理', icon: 'schedule', roles: [roleConstant.eduManager] },
    children: [
      {
        path: 'teach-time',
        component: () => import('@/views/course/schedule/teachTime'),
        meta: { title: '教学时段质量管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'auto-schedule',
        component: () => import('@/views/course/schedule/courseSchedule'),
        meta: { title: '排课', roles: [roleConstant.eduManager] }
      }
    ]
  },
  // 教学管理
  {
    path: '/teach',
    component: Layout,
    meta: { title: '教学管理', icon: 'teach', roles: [roleConstant.eduManager] },
    children: [
      {
        path: 'recource',
        component: () => import('@/views/course/teach/resource'),
        meta: { title: '课程资源管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'homework',
        component: () => import('@/views/course/teach/homework'),
        meta: { title: '课程作业管理', roles: [roleConstant.eduManager] }
      },
      {
        path: 'major-plan',
        component: () => import('@/views/course/teach/majorPlan'),
        meta: { title: '专业培养计划管理', roles: [roleConstant.eduManager] }
      }

    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// TODO 路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to.path)
  // console.log(from.path)
  next()
})

export default router
